#!/bin/bash
remote_tag=$1
port=$2
docker_namespace=$3
module_name=$4
DOCKER_USERNAME=$5
DOCKER_PASSWORD=$6
# 登陆harbor
echo "$DOCKER_PASSWORD" | docker login http://registry.cn-shenzhen.aliyuncs.com -u "$DOCKER_USERNAME" --password-stdin
# 停掉容器
if [ -n "$docker_namespace" -a -n "$module_name" ]
then
  docker stop $(docker ps -a | grep "${module_name}" | awk '{print $1}') || true
  docker rm -f $(docker ps -a | grep "${module_name}" | awk '{print $1}') || true
  docker rmi -f $(docker images -a | grep "${module_name}" | awk '{print $3}') || true
fi
# 拉取最新镜像
docker pull "${remote_tag}"
TAG=$(echo "${remote_tag}" | awk -F':' '{print $2}')
# 运行容器
docker run -d \
  --name=${module_name}-${TAG} \
  -p ${port}:${port} \
  --net=host \
  -v /usr/local/maple/logs:/logs \
  -v /usr/local/skywalking/agent:/skywalking/agent \
  --restart=always \
  ${remote_tag}
